package v3.chapter2.synchronizedMethodLockObject;

public class Run {

	public static void main(String[] args) {
		MyObject object=new MyObject();
		ThreadA a=new ThreadA(object);
		a.setName("A");
		
		ThreadA b=new ThreadA(object);
		b.setName("B");
		
		a.start();
		b.start();

	}

}

class ThreadA extends Thread{
	private MyObject object;
	public ThreadA(MyObject object) {
		super();
		this.object=object;
	}
	
	@Override
	public void run() {
		super.run();
		object.methodA();
	}
}


class ThreadB extends Thread{
	private MyObject object;
	public ThreadB(MyObject object) {
		super();
		this.object=object;
	}
	
	@Override
	public void run() {
		super.run();
		object.methodA();
	}
}

class MyObject{
	synchronized public void methodA() {
	//public void methodA() {
		try {
			System.out.println("begin methodA threadName="+Thread.currentThread().getName());
			Thread.sleep(5000);
			System.out.println("end");
		}catch(InterruptedException e) {
			e.printStackTrace();
		}
	}
}